package com.yangguang.huahui.ui.fragments;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.yangguang.huahui.R;
import com.yangguang.huahui.ui.adapters.TypePageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/4/17.
 */

public class CollectFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;

    private List<BaseFragment> mFragments = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private TypePageAdapter mTypeAdapter;
    @Override
    protected int initLayoutId() {
        return R.layout.frag_collect;
    }

    @Override
    protected void initView() {
        mTabLayout.removeAllTabs();
        mTitles.clear();
        mFragments.clear();
        mTabLayout.addTab(mTabLayout.newTab().setText(getString(R.string.client_need)));
        mTabLayout.addTab(mTabLayout.newTab().setText(getString(R.string.find_miaomu)));
        mTabLayout.addTab(mTabLayout.newTab().setText(getString(R.string.find_huahui)));
        mTabLayout.addTab(mTabLayout.newTab().setText(getString(R.string.find_nongzi)));
        mTabLayout.addTab(mTabLayout.newTab().setText(getString(R.string.find_nongji)));
        int[] types = {1, 8, 9, 10, 23};
        for (int i = 0; i< mTabLayout.getTabCount();i++){
            mTitles.add(mTabLayout.getTabAt(i).getText().toString());
            mFragments.add(CollectionListFragment.newInstance(types[i]));
        }
        setPagerAdapter(mFragments, mTitles);
    }

    @Override
    protected void initData() {

    }

    private void setPagerAdapter(List<BaseFragment> mFragments, List<String> mTitles) {
        mTypeAdapter = new TypePageAdapter(getChildFragmentManager());
        mTypeAdapter.setData(mFragments, mTitles);
        mViewPager.setAdapter(mTypeAdapter);
        mViewPager.setOffscreenPageLimit(mTitles.size() - 1);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(mTypeAdapter);
        mTabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
